/*
	Pulsar engine. Core.
	Additional include file. String conversions

	Copyright (C) 2013-2014 Dmitry Dolgov

	This program is free software; you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation; either version 2 of
	the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	See the GNU General Public License for more details.
*/

namespace pl
{

// Default string type for UTF8 based strings
typedef unsigned char pl_char8;

// Convert from ANSI to UNICODE
extern void convertChar(char c, wchar_t& res);
extern void convertStr(const char* src, wchar_t* dest, pl_uint32 destSize);

// Convert from ANSI to UNICODE32
inline void convertChar(char c, pl_uint32& res)
{
	wchar_t res1;
	convertChar(c, res1);
	res = res1;
	return;
}

// Convert from UNICODE to ANSI
extern void convertChar(wchar_t c, char& res);
extern void convertStr(const wchar_t* src, char* dest, pl_uint32 destSize);

// Convert from UNICODE32 to ANSI
inline void convertChar(pl_uint32 c, char& res)
{
	convertChar((wchar_t)c, res);
	return;
}

// Convert from ANSI to UTF8
extern int convertChar(char c, pl_char8* buf);
extern void convertStr(const char* src, pl_char8* dest, pl_uint32 destSize);

// Convert from UTF8 to ANSI
extern void convertChar(const pl_char8* src, char& res);
void convertStr(const pl_char8* src, char* dest, pl_uint32 destSize);

// Convert from UNICODE to UTF8
extern int convertChar(wchar_t c, pl_char8* buf);
void convertStr(const wchar_t* src, pl_char8* dest, pl_uint32 destSize);

// Convert from UTF8 to UNICODE
extern void convertChar(const pl_char8* src, wchar_t& res);
void convertStr(const pl_char8* src, wchar_t* dest, pl_uint32 destSize);

// Stub conversions
inline void convertChar(char c, char& res)
{
	res = c;
	return;
}
inline void convertStr(const char* src, char* dest, pl_uint32 destSize)
{
	strncpy(dest, src, destSize);
	dest[destSize-1] = '\0';
	return;
}

inline void convertChar(wchar_t c, wchar_t& res)
{
	res = c;
	return;
}
inline void convertStr(const wchar_t* src, wchar_t* dest, pl_uint32 destSize)
{
	wcsncpy(dest, src, destSize);
	dest[destSize-1] = L'\0';
	return;
}

inline void convertChar(pl_uint32 c, wchar_t& res)
{
	res = (wchar_t)c;
	return;
}
inline void convertChar(wchar_t c, pl_uint32& res)
{
	res = c;
	return;
}

inline void convertStr(const pl_char8* src, pl_char8* dest, pl_uint32 destSize)
{
	strncpy((char*)dest, (char*)src, destSize);
	dest[destSize-1] = 0;
	return;
}

// Default codepage CP1251 (Cyrillic)
extern pl_uint16 _codepage1251[];

// Set current codepage
extern void setCurrentCodepage(const pl_uint16* translationTable);

} // namespace pl

//
// End of file 'pulsar_string_convert.h'
//
